package flex.cc.system.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import flex.cc.PublicUtil;
import flex.cc.base.dto.LoginAuthDto;
import flex.cc.system.dao.DictCodeDao;
import flex.cc.system.entity.DictCodeEntity;
import flex.cc.system.service.DictCodeService;
import flex.cc.system.vo.DictCodeVo;
import flex.cc.system.vo.DictOptionVo;


@Service("dictCodeService")
public class DictCodeServiceImpl extends ServiceImpl<DictCodeDao, DictCodeEntity> implements DictCodeService {

	@Autowired
	private DictCodeDao dictCodeDao;

	/**
	 * 新增或修改code信息
	 */
	@Override
	public String saveOrUpdateDictCode(DictCodeVo vo,LoginAuthDto loginAuthDto) {
		//根据机构 数据字典type code查询数据字典信息
		List<DictCodeEntity> list = dictCodeDao.getdictCodeList2(vo);
		DictCodeEntity dictCodeEntity = new DictCodeEntity();
		BeanUtils.copyProperties(vo,dictCodeEntity);
		boolean updateFlag = false;
		for (DictCodeEntity dictCodeEntity2 : list) {
			if("1".equals(dictCodeEntity2.getSystemFlag())) {//系统版本 不允许修改和新增版本
				updateFlag = true;
			}
		}
		if(updateFlag) {//系统版本 不允许修改和新增版本
			return "此code已存在且为系统版本，不允许新增或修改";
		}else {
			dictCodeEntity.setUpdateInfo(loginAuthDto);
			if(PublicUtil.isEmpty(dictCodeEntity.getId())) {//新增
				if(PublicUtil.isEmpty(list)){
					if(PublicUtil.isEmpty(dictCodeEntity.getValidStatus())) {
						dictCodeEntity.setValidStatus("1");
					}
					dictCodeEntity.setTypeVersion(1);
					if(!"1".equals(dictCodeEntity.getSystemFlag())) {
						dictCodeEntity.setSystemFlag("0");
					}
					this.save(dictCodeEntity);
					return "新增成功";
				}else {//新增了相同code
					return "此code已存在，不允许新增";
				}
			}else {//修改code
				boolean updateFlag2 = false;
				for (DictCodeEntity dictCodeEntity2 : list) {
					if(dictCodeEntity.getId().equals(dictCodeEntity2.getId())) {//修改自己
						updateFlag2 = true;
						break;
					}
				}
				if(updateFlag2) {
					if(vo.getIsNewVersion()) {//新增版本
						dictCodeEntity.setTypeVersion(dictCodeEntity.getTypeVersion()+1);
						this.updateById(dictCodeEntity);
						return "新增版本成功";
					}else {
						this.updateById(dictCodeEntity);
						return "修改成功";
					}
				}else {//把别的code修改为已存在的code 不允许
					return "此code已存在，不允许修改";
				}
			}
		}
	}

	/**
	 * 根据type、code和comcode查询数据字典副类型信息
	 */
	@Override
	public DictCodeEntity searchAllCodeByTypeCode(String type,String code,int typeVersion, String comCode) {
		DictCodeVo vo = new DictCodeVo();
		vo.setTypeCode(type);
		vo.setComCode(comCode);
		vo.setCode(code);
		vo.setTypeVersion(typeVersion);
		List<DictCodeEntity> list = dictCodeDao.getdictCodeList2(vo);
		if(PublicUtil.isNotEmpty(list)) {
			DictCodeEntity one = list.get(0);
			return one;
		}
		return null;
	}

	/**
	 * 根据页面所传条件查询数据字典类型信息-打开编辑type页面
	 */
	@Override
	public List<DictCodeEntity> searchAllCode(DictCodeEntity dictCodeEntity,LoginAuthDto loginAuthDto) {
		List<DictCodeEntity> list = dictCodeDao.getdictCodeList(dictCodeEntity);
		return list;
	}

	/**
	 * 根据type和comCode查询数据字典副类型信息-list
	 */
	@Override
	public List<DictCodeEntity> searchCodeByTypeList(String type,int typeVersion,String comCode) {
		DictCodeEntity dictCodeEntity = new DictCodeEntity();
		dictCodeEntity.setTypeCode(type);
		dictCodeEntity.setComCode(comCode);
		dictCodeEntity.setTypeVersion(typeVersion);
		List<DictCodeEntity> list = dictCodeDao.getdictCodeList(dictCodeEntity);
		return list;
	}

	@Override
	public List<Map<String, String>> searchSelectOptionsByType(String type, int typeVersion, String comCode) {
		DictCodeVo vo = new DictCodeVo();
		vo.setTypeCode(type);
		vo.setComCode(comCode);
		vo.setTypeVersion(typeVersion);
		List<Map<String, String>> maps = dictCodeDao.getdictCodeList3(vo);
		return maps;
	}

	/**
	 * 根据type和comCode查询数据字典副类型信息-map
	 */
	@Override
	public Map<String, Object> searchCodeByTypeMap(String type,int typeVersion,String comCode) {
		DictCodeEntity dictCodeEntity = new DictCodeEntity();
		dictCodeEntity.setTypeCode(type);
		dictCodeEntity.setComCode(comCode);
		dictCodeEntity.setTypeVersion(typeVersion);
		Map<String, Object> map = dictCodeDao.getdictCodeMap(dictCodeEntity);
		return map;
	}

	/**
	 * 注销数据字典副类型
	 */
	@Override
	public String updateDictTypeById(DictCodeEntity dictCodeEntity,LoginAuthDto loginAuthDto) {
		DictCodeEntity po = new DictCodeEntity();
		po.setId(dictCodeEntity.getId());
		po.setValidStatus(dictCodeEntity.getValidStatus());
		po.setUpdateInfo(loginAuthDto);
		po.setUpdateTime(new Date());
		this.updateById(po);
		return "1";
	}

	/**
	 * 逻辑删除数据字典code
	 * @param id
	 * @return
	 */
	@Override
	public boolean deleteDictCodeById(Long id, LoginAuthDto loginAuthDto) {
		DictCodeEntity dictCodeEntity = new DictCodeEntity();
		//把操作座席的工号更新上再删除
		dictCodeEntity.setUpdateInfo(loginAuthDto);
		dictCodeEntity.setId(id);
		this.updateById(dictCodeEntity);
		this.removeById(id);
		return true;
	}

	/**
	 * 根据typeCode查询数据字典副类型-打开编辑type页面
	 */
	@Override
	public IPage<DictCodeEntity> getdictCodeList(DictCodeEntity dictCodeEntity) {
		IPage<DictCodeEntity> page =dictCodeDao.getdictCodeList(new Page(dictCodeEntity.getPageNum(),dictCodeEntity.getPageSize()),dictCodeEntity);
		return page;
	}

	/**
	 * 根据type查询数据字典code，用来显示下拉选项
	 * @param dicType
	 * @param validStatus：ALL\0\1，默认为1
	 * @return
	 */
	public List<DictOptionVo> getSelectOptions(String dicType, String validStatus){
		validStatus = PublicUtil.isNotEmpty(validStatus) ? validStatus : "1";
		return dictCodeDao.getSelectOptions(dicType, validStatus);
	}
	
	/**
	 * 查询行政区划下拉选项的值
	 * @param parentId 上级编号
	 * @return
	 */
	public List<DictOptionVo> getAdmOptions(String parentIds){
		return dictCodeDao.getAdmOptions(parentIds);
	}

	/**
	*
	* @author WANGCONG
	* 2021年12月24日下午12:49:12
	* @desc 实体信息获取
	*/
	public DictCodeEntity getOneByParams(Map<String, Object> params){
		return dictCodeDao.getOneByParams(params);
	}

	/**
	 * @description 根据参数查询字典数据列表
	 * @param params 查询参数
	 * @return
	 * @author wkangle
	 * @time 2021/12/31 0031 17:06
	 * @exception/throws
	 */
	public List<DictCodeEntity> queryDictCodeListByParams(Map<String, Object> params) {
		return dictCodeDao.queryDictCodeListByParams(params);
	}

	/**
	 * @return
	 * @description 查询卡券节日提示开关
	 * @author wkangle
	 * @time 2022/01/11 0011 14:06
	 * @exception/throws
	 */
	@Override
	public DictCodeEntity queryCouponFestivalTips() {
		return dictCodeDao.queryCouponFestivalTips();
	}

	/**
	 * @description 查询咨询类卡券企业维系
	 * @param
	 * @return
	 * @author wkangle
	 * @time 2022/02/11 0011 13:34
	 * @exception/throws
	 */
	@Override
	public DictCodeEntity queryConsultWorkWeiXin(Map<String, String> params) {
		return dictCodeDao.queryConsultWorkWeiXin(params);
	}

	/**
	 * 查询行政区划下拉选项的值
	 * @param level 所属等级
	 * @return
	 */
	@Override
	public List<DictOptionVo> getAdmOptionsByLeval(String level) {
		return dictCodeDao.getAdmOptionsByLeval(level);
	}

}
